<template>
    <view>
        <!-- pages/cleaner/cleaner.wxml -->
        <view class="tabs">
            <van-dropdown-menu :active-color="mainColor">
                <van-dropdown-item :value="storeId" :options="stores" @change="storeDropdown" />
            </van-dropdown-menu>
        </view>
        <view class="container" :style="isIpx ? 'padding-bottom:168rpx' : 'padding-bottom:120rpx'">
            <block v-if="list.length">
                <!-- 列表 -->
                <view class="list">
                    <view class="item" v-for="(item, index) in list" :key="index">
                        <view class="top">
                            <view class="left">
                                <view class="img">
                                    <image :src="item.avatar ? item.avatar : '/logo.png'" mode="widthFix"></image>
                                </view>
                                <view class="nick">{{ item.nickname }}</view>
                                <!-- <view class="del color-attention" bindtap="delete" data-info="{{item}}">删除</view> -->
                            </view>
                            <view class="right">
                                <view class="info-box">
                                    <view class="info">门店：{{ item.storeName }}</view>
                                    <view class="info">姓名：{{ item.name }}</view>
                                    <view class="info">手机号：{{ item.mobile }}</view>
                                    <view class="info">总收入：{{ item.money }}元</view>
                                    <view class="info">已完成：{{ item.finishCount }}单,已结算：{{ item.settlementCount }}单</view>
                                </view>
                                <view class="btns">
                                    <view class="btn green" :data-info="item" @tap="taskSettle">任务结算</view>
                                    <view class="btn red" :data-info="item" @tap="deleteFun">删除</view>
                                </view>
                            </view>
                        </view>
                    </view>
                </view>
                <view v-if="canLoadMore" class="noteMore">下拉刷新查看更多...</view>
            </block>
            <block v-else>
                <view class="nodata">暂无数据</view>
            </block>
        </view>
        <!-- 底部按钮 -->
        <view :class="'bottom bg-primary ' + (isIpx ? 'fix-iphonex-button' : '')" @tap="add">添加保洁员</view>
        <!-- 添加弹窗 -->
        <van-dialog
            use-slot
            title="添加保洁员"
            :show="show"
            show-cancel-button
            @confirm="submit"
            @cancel="cancel"
            :before-close="beforeCloseFunction"
            :confirm-button-color="mainColor"
        >
            <view class="dialog">
                <van-cell-group>
                    <van-field label="门店：" placeholder="请选择门店">
                        <picker slot="input" @change="bindStoreChange" :value="index" range-key="text" :range="stores">
                            <view class="picker">
                                <input type="text" disabled placeholder="请选择门店" placeholder-class="plh" :value="stores[index].text" />
                                <van-icon name="arrow-down" />
                            </view>
                        </picker>
                    </van-field>
                    <van-field v-model="name" label="姓名：" placeholder="请输入" />
                    <van-field v-model="mobile" label="手机号：" placeholder="请输入" :border="false" />
                </van-cell-group>
            </view>
        </van-dialog>
    </view>
</template>

<script>
// pages/cleaner/cleaner.js
const app = getApp();
var http = require('../../../utils/http');
var util1 = require('../../../utils/util.js');
export default {
    data() {
        return {
            storeId: '',

            //列表搜索门店id
            stores: [],

            show: false,

            //添加弹窗
            index: '',

            pageNo: 1,
            pageSize: 10,
            canLoadMore: true,

            //是否还能加载更多
            list: [],

            mobile: '',
            name: '',
            beforeCloseFunction: null,
            isIpx: app.globalData.isIpx ? true : false,
            mainColor: app.globalData.mainColor,
            text: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad(options) {
        this.getXiaLaListAdmin();
        this.setData({
            beforeCloseFunction: this.beforeClose()
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        let that = this;
        this.setData({
            pageNo: 1,
            canLoadMore: true,
            list: []
        });
        this.getMainListdata('refresh');
        uni.stopPullDownRefresh();
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        let that = this;
        if (that.canLoadMore) {
            that.pageNo++;
            this.getMainListdata('');
        } else {
            uni.showToast({
                title: '我是有底线的...'
            });
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
    methods: {
        //管理员获取门店下拉列表数据
        getXiaLaListAdmin: function (e) {
            var that = this;
            //if (app.globalData.isLogin)
            {
                http.request(
                    '/member/store/getStoreList',
                    '1',
                    'get',
                    {},
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('下拉门店数据===');
                        console.info(info);
                        if (info.code == 0) {
                            let stores = [];
                            info.data.map((it) => {
                                stores.push({
                                    text: it.key,
                                    value: it.value
                                });
                            });
                            that.setData({
                                stores: stores,
                                storeId: stores[0].value,
                                index: 0
                            });
                            that.getMainListdata('refresh');
                        } else {
                            uni.showModal({
                                content: '请求服务异常，请稍后重试',
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        //获取列表数据
        getMainListdata: function (e) {
            var that = this;
            let message = '';
            if (app.globalData.isLogin) {
                if (e == 'refresh') {
                    //刷新，page变为1
                    message = '正在加载';
                    that.setData({
                        pageNo: 1
                    });
                }
                http.request(
                    '/member/manager/getClearUserPage',
                    '1',
                    'post',
                    {
                        pageNo: that.pageNo,
                        pageSize: that.pageSize,
                        storeId: that.storeId
                    },
                    app.globalData.userDatatoken.accessToken,
                    message,
                    function success(info) {
                        console.info('返回111===');
                        console.info(info);
                        if (info.code == 0) {
                            if (e == 'refresh') {
                                that.setData({
                                    list: info.data.list
                                });
                                if (info.data.list.length === 0) {
                                    that.setData({
                                        canLoadMore: false
                                    });
                                }
                            } else {
                                if (info.data != null && info.data.list.length <= info.data.total) {
                                    that.setData({
                                        canLoadMore: false
                                    });
                                }
                                let arr = that.list;
                                let arrs = arr.concat(info.data.list);
                                that.setData({
                                    list: arrs
                                });
                            }
                        } else {
                            uni.showModal({
                                content: '请求服务异常，请稍后重试',
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        //门店下拉菜单发生变化
        storeDropdown(event) {
            //console.log(event)
            this.stores.map((it, index) => {
                if (it.value == event.detail) {
                    this.setData({
                        storeId: it.value,
                        index: index
                    });
                }
            });
            this.getMainListdata('refresh');
        },

        // 删除保洁员
        deleteFun: function (e) {
            let info = e.currentTarget.dataset.info;
            uni.showModal({
                title: '提示',
                content: '员工在' + info.storeName + '完成的任务需全部结算后才允许删除！删除后该员工将无法在此门店接单！请确认！',
                confirmText: '确认删除',
                complete: (res) => {
                    if (res.cancel) {
                    }
                    if (res.confirm) {
                        let that = this;
                        if (app.globalData.isLogin) {
                            http.request(
                                '/member/manager/deleteClearUser/' + info.storeId + '/' + info.userId,
                                '1',
                                'post',
                                {
                                    storeId: info.storeId,
                                    userId: info.userId
                                },
                                app.globalData.userDatatoken.accessToken,
                                '保存中',
                                function success(info) {
                                    console.info('返回111===');
                                    console.info(info);
                                    if (info.code == 0) {
                                        uni.showToast({
                                            title: '删除成功'
                                        });
                                        that.getMainListdata('refresh');
                                    } else {
                                        uni.showModal({
                                            content: info.msg,
                                            showCancel: false
                                        });
                                    }
                                },
                                function fail(info) {
                                    uni.showModal({
                                        content: '请求服务异常，请稍后重试',
                                        showCancel: false
                                    });
                                }
                            );
                        }
                    }
                }
            });
        },

        // 添加
        add() {
            this.setData({
                show: true
            });
        },

        bindStoreChange: function (e) {
            //console.log('picker发送选择改变，携带值为', e.detail.value)
            this.setData({
                index: e.detail.value,
                storeId: this.stores[e.detail.value].value
            });
        },

        // 保存
        submit: function () {
            if (this.storeId && this.name && this.mobile) {
                this.updateData();
            } else {
                uni.showToast({
                    title: '请输入完整',
                    icon: 'none'
                });
            }
        },

        // 取消
        cancel: function () {
            this.setData({
                name: '',
                mobile: ''
            });
        },

        beforeClose() {
            // 这里一定要用箭头函数，否则访问不到this
            return (type) => {
                //console.log(type)
                if (type === 'cancel') {
                    // 点击取消
                    return true;
                } else {
                    // 点击确定
                }
            };
        },

        // 修改调接口
        updateData: function () {
            let that = this;
            if (app.globalData.isLogin) {
                http.request(
                    '/member/manager/saveClearUser',
                    '1',
                    'post',
                    {
                        storeId: that.storeId,
                        name: that.name,
                        mobile: that.mobile
                    },
                    app.globalData.userDatatoken.accessToken,
                    '保存中',
                    function success(info) {
                        console.info('返回111===');
                        console.info(info);
                        if (info.code == 0) {
                            uni.showToast({
                                title: '保存成功'
                            });
                            that.setData({
                                show: false
                            });
                            that.getMainListdata('refresh');
                        } else {
                            uni.showModal({
                                content: info.msg,
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {
                        uni.showModal({
                            content: '请求服务异常，请稍后重试',
                            showCancel: false
                        });
                    }
                );
            }
        },

        // 去结算
        taskSettle: function (e) {
            let info = e.currentTarget.dataset.info;
            uni.navigateTo({
                url: '../taskSettle/taskSettle?info=' + JSON.stringify(info)
            });
        }
    }
};
</script>
<style>
@import './cleaner.css';
</style>
